import Hero from "./Hero";
import { Collisione2dUtil } from "./libs/Collision2dUtil";
import MapTool from "./libs/MapTool";
import { QuadTree } from "./libs/QuadTree";
import { Rect } from "./libs/Rect";

const { ccclass, property } = cc._decorator;


@ccclass
export default class Map extends cc.Component {

    qTree: QuadTree;

    wallNode: cc.Node;

    @property(cc.Node)
    sMap: cc.Node = null;

    onLoad() {

    }

    start() {
        this.qTree = new QuadTree(0, new Rect(0, -384 * 3, 384 * 4, 384 * 4));
        this.initMap(new MapTool().createMap());
    }

    initMap(mapArr: Array<Array<string>>): void {

        let mapStart: cc.Vec2;//初始位置

        for (let i = 0; i < mapArr.length; i++) {
            for (let j = 0; j < mapArr[i].length; j++) {
                let mapId = mapArr[i][j];

                if (!mapId || mapId == "00000") continue;

                if (!mapStart) {
                    mapStart = cc.v2(i, j);
                }

                cc.loader.loadRes(`map/${mapId}`, cc.TiledMapAsset, (err, res) => {

                    if (err) {
                        console.warn("资源加载失败");
                        return;
                    }

                    let node = new cc.Node();

                    this.wallNode = node;

                    node.anchorX = node.anchorY = 0;

                    node.x = (j - mapStart.y) * 384;
                    node.y = -(i - mapStart.x) * 384;

                    this.node.addChild(node);

                    let tilemap = node.addComponent(cc.TiledMap);

                    tilemap.tmxAsset = res;

                    this.initMapNode(node);
                });
            }
        }
    }

    initMapNode(node: cc.Node): void {

        let tiledMap = node.getComponent(cc.TiledMap);

        let wallLayer = tiledMap.getLayer("wall");

        let layerSize = wallLayer.getLayerSize();

        for (let i = 0; i < layerSize.width; i++) {
            for (let j = 0; j < layerSize.height; j++) {

                let tiled: cc.TiledTile = wallLayer.getTiledTileAt(i, j, true);

                if (tiled.gid != 0) {

                    tiled.node.group = "wall";

                    let worldVec = tiled.node.parent.convertToWorldSpaceAR(cc.v2(tiled.node.x, tiled.node.y));

                    let mapVec = this.node.convertToNodeSpaceAR(worldVec);

                    this.qTree.insert({ target: tiled, rect: new Rect(mapVec.x, mapVec.y, 16, 16) });

                }
            }
        }
    }

    checkCollision(worldPos: cc.Vec3) {
        let mapVec = this.node.convertToNodeSpaceAR(worldPos);
        let rect = new Rect(mapVec.x, mapVec.y, 10, 10);
        let objArr = this.qTree.findObjects<cc.TiledTile>(rect);

        for (let i = 0; i < objArr.length; i++) {
            let bool = Collisione2dUtil.aabbCollision(rect, objArr[i].rect);
            if (bool) {
                objArr[i].target.node.color = new cc.Color(255, 0, 0);
                return true;
            }
        }
        return false;
    }

    showSMap(): void {
        let texture = new cc.RenderTexture();

        texture.initWithSize(this.node.width, this.node.height);

        // this.node.

        // texture.readPixels(this.node.text)
    }


}